package com.ricky.web.util;

import com.jcraft.jsch.JSchException;
import com.ricky.base.bean.SshClient;
import com.ricky.base.util.Sm4Util;
import com.ricky.web.table.Server;
import com.ricky.web.table.Service;

import java.util.List;

/**
 * @author Ricky
 * @date 2020/12/1
 */
public class SshUtil {

    public static SshClient getSshClientByServer(Server server) throws JSchException {
        String host = server.getHost();
        String username = server.getUsername();
        String password = Sm4Util.decrypt(server.getPassword(), "password");
        int port = server.getPort();
        return new SshClient(host, username, password, port);
    }

    public static List<String> getPidListByService(Service service) throws Exception {
        List<String> pidList;
        try (SshClient sshClient = getSshClientByServer(service.getServer())) {
            pidList = sshClient.execToList("ps -ef | grep " + service.getFileName() + " | grep -v grep | cut -c 10-15");
        }
        return pidList;
    }
}
